package org.ruiyichen.guizhou.agr.ui.mineview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.widget.TextView;

public class ShowTextView extends TextView {

    public static final String FORMAT_SIGN = "%";
    public static final String FORMAT_COIN = "¥";
    public static final String FORMAT_DAY = "天";

    public ShowTextView(Context context) {
        super(context);
    }

    public ShowTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ShowTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ShowTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setShowText(String text) {
        if (text == null) return;

//        String str = "8%-6%666%";
        int index = 0;

        SpannableString sb = new SpannableString(text);

        while ((index = text.indexOf(FORMAT_SIGN, index + 1)) > 0) {
            sb.setSpan(new RelativeSizeSpan(0.5f), index, index + FORMAT_SIGN.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        while ((index = text.indexOf(FORMAT_COIN, index + 1)) > 0) {
            sb.setSpan(new RelativeSizeSpan(0.5f), index, index + FORMAT_COIN.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        while ((index = text.indexOf(FORMAT_DAY, index + 1)) > 0) {
            sb.setSpan(new RelativeSizeSpan(0.5f), index, index + FORMAT_COIN.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        setText(sb);
    }

    public void setShowText(String text, String tag) {
        if (text == null) return;
        if (tag == null) {
            setText(text);
            return;
        }
        int index = 0;

        SpannableString sb = new SpannableString(text);

        while ((index = text.indexOf(tag, index + 1)) > 0) {
            sb.setSpan(new RelativeSizeSpan(0.5f), index, index + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        setText(sb);
    }
}
